package com.love.marriage.userBehaviorInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.userBehaviorInfo.entity.UserBehaviorInfo;
import com.love.marriage.userBehaviorInfo.mapper.UserBehaviorInfoMapper;
import com.love.marriage.userBehaviorInfo.service.UserBehaviorInfoService;
import com.love.marriage.userBehaviorInfo.vo.UserBehaviorInfoStatisticsVo;
import com.love.marriage.userBehaviorInfo.vo.UserBehaviorInfoVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  用户行为信息表服务实现类
 * author:       alex
 * createDate:   2024-10-14 23:30:59
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class UserBehaviorInfoServiceImp extends ServiceImpl<UserBehaviorInfoMapper, UserBehaviorInfo> implements UserBehaviorInfoService {

    private final UserBehaviorInfoMapper userBehaviorInfoMapper;

    @Override
    public Page<UserBehaviorInfoVo> getPage(Long pageNum, Long pageSize, UserBehaviorInfoVo userBehaviorInfoVo) {
        Page<UserBehaviorInfoVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return userBehaviorInfoMapper.getPage(page, userBehaviorInfoVo);
    }

    @Override
    public UserBehaviorInfoVo queryUserBehaviorInfo(Long id) {
        return userBehaviorInfoMapper.queryUserBehaviorInfo(id);
    }

    public List<UserBehaviorInfoVo> queryUserBehaviorInfo(UserBehaviorInfoVo userBehaviorInfoVo) {
        return userBehaviorInfoMapper.queryUserBehaviorInfoInfo(userBehaviorInfoVo);
    }

    @Override
    public UserBehaviorInfoVo addUserBehaviorInfo(UserBehaviorInfoVo userBehaviorInfoVo) {
        if (StringUtils.isBlank(userBehaviorInfoVo.getActionType())) {
            return null;
        }
        List<UserBehaviorInfoVo> db;
        switch (userBehaviorInfoVo.getActionType()) {
            case "unlike":
                userBehaviorInfoVo.setActionType("like");
                db = queryUserBehaviorInfo(userBehaviorInfoVo);
                if (db != null && !db.isEmpty()) {
                    userBehaviorInfoMapper.deleteById(db.get(0).getId());
                }
                return null;
            case "noCollect":
                userBehaviorInfoVo.setActionType("collect");
                db = queryUserBehaviorInfo(userBehaviorInfoVo);
                if (db != null && !db.isEmpty()) {
                    userBehaviorInfoMapper.deleteById(db.get(0).getId());
                }
                return null;
            case "like":
            case "collect":
                db = queryUserBehaviorInfo(userBehaviorInfoVo);
                if (db != null && !db.isEmpty()) {
                    return null;
                }
            default:
                UserBehaviorInfo userBehaviorInfo = new UserBehaviorInfo();
                BeanUtils.copyProperties(userBehaviorInfoVo, userBehaviorInfo);
                userBehaviorInfoMapper.insert(userBehaviorInfo);
                UserBehaviorInfoVo res = new UserBehaviorInfoVo();
                BeanUtils.copyProperties(userBehaviorInfo, res);
                return res;
        }
    }

    @Override
    public UserBehaviorInfoVo updateUserBehaviorInfo(UserBehaviorInfoVo userBehaviorInfoVo) {
        UserBehaviorInfo userBehaviorInfo = new UserBehaviorInfo();
        BeanUtils.copyProperties(userBehaviorInfoVo, userBehaviorInfo);
        userBehaviorInfoMapper.updateById(userBehaviorInfo);
        return userBehaviorInfoVo;
    }

    @Override
    public Boolean deleteUserBehaviorInfo(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        userBehaviorInfoMapper.deleteBatchIds(idArr);
        return true;
    }

    @Override
    public UserBehaviorInfoStatisticsVo queryUserBehaviorStatisticsInfo(Long userId) {
        return userBehaviorInfoMapper.queryUserBehaviorStatisticsInfo(userId);
    }
}
